/**
 * Author: Shen Yanqiu
 * Date: 2017-12-07
 * Time: 16:07
 *
 */
import { routerRedux } from "dva/router";
import { routerTool } from 'utils';
import modelExtend from 'dva-model-extend';
import { pageModel } from '../common';
import { getActivityList, getActivityDetails } from './../../services/activity';

export default modelExtend(pageModel, {
  namespace: 'activity',

  state: {
    detailsInfo: {
    },
  },

  subscriptions: {
    setup ({ dispatch, history }) {
      history.listen((location) => {
        // console.log(location.query);
        // console.log('活动 订阅');
        if (location.pathname === '/activity') {
          dispatch({ type: 'getActivityList' });
        }
      });
    },
  },

  effects: {
    * getActivityList ({ payload = {} }, { call, put }) {
      // console.log(payload);
      const { success, data } = yield call(getActivityList, {
        page: payload.current,
        pageSize: payload.pageSize,
        name: payload.name || undefined,
        dept: payload.dept || undefined,
      });
      if (success && data) {
        yield put({
          type: 'loadSuccess',
          payload: {
            list: data.list,
            pagination: {
              current: Number(payload.current) || 1,
              pageSize: Number(payload.pageSize) || 10,
              total: data.total,
            },
          },
        });
      }
    },
    * onSeeItemModal ({ payload: id }, { call, put }) {
      // yield put(
      //   routerRedux.push({ pathname: '/activityDetails/' + id.id,
      //   })
      // );
      yield put(routerTool.redirectTo({
        name: 'ActivityDetails',
        params: {
          id: id.id,
        },
      }));
    },
    * onDeleteItemModal ({ payload: id }) {
      console.log('delete' + id.id);
    },
    * searchData({ payload: formData }) {
      console.log(formData);
    },
    * refreshDetails ({ payload: id }, { call, put }) {
      const { data } = yield call(getActivityDetails, id);
      yield put({ type: 'update', payload: { data } });
    },
  },

  reducers: {
    update (state, { payload: { data: detailsInfo } }) {
      return { ...state, detailsInfo };
    },
  },
});
